require(['validator'], function () {
    var $right = $("#sysCodeDsc #right"),
        corpId = "no", codeClId = "", codeClDsc = "";
    var $left = $('#left');
    var $sysCodeDscForm = $('#sysCodeDscForm');
    $sysCodeDscForm.initForm();
    // 左侧树
    commonFun.leftTreeNodeClickObj['sysCodeDescSet'] = function (event, treeId, treeNode) {
        corpId = treeNode.corpId;
        if (treeNode.level == 0) {
            corpId = "";
        }
        $("#codeCorpId").val(corpId);
        initLeft();
        initRight();
    };


    $left.on('click', 'li', function () {
        if (corpId == "no") {
            $.messager.alert("提示", "请选择左侧树，进行查询操作。", "info");
        } else {
            if (this.className.indexOf('active') == -1) {

                $(this).addActiveClass();
                codeClId = $(this).attr('codeClId');
                codeClDsc = $(this).attr('codeClDsc');
                $("#codeClId").val(codeClId);
                $("#codeClDsc").val(codeClDsc);
                initRight();
            }
        }
    });

    $(window).on('resize', function () {
        $left.height($(window).height() - 67);
    }).trigger('resize');

    // 初始化左侧
    var initLeft = function () {

        $.ajaxExt({
            url: "/sysCodeDsc/getSysCodeDscCategoryName.htm"
        }).done(function (data) {
            $left.empty();
            $.each(data, function (n, sysCodeDsc) {
                $left.append("<li class='bbh' codeClId='" + sysCodeDsc.codeClId + "' codeClDsc='" + sysCodeDsc.codeClDsc + "'><a>" + sysCodeDsc.codeClDsc + "</a></div>");
            });
            $("#r-tip").show();
            $("#r-tip").html("选择左侧的代码描述类别，设置对应的代码描述信息。");
            $("#showData").hide();
            $("#r-title").hide();
            $("#sysCodeDsc_bnt").hide();
        });
    };

    // 界面初始化
    var initInterface = function () {
        initLeft();
    };
    // 初始化右侧
    var initRight = function () {
        $.ajaxExt({
            async: false,
            url: "/sysCodeDsc/searchSysCodeDscByCodeClId.htm",
            data: {"corpId": corpId, "qcodeClId": codeClId}
        }).done(function (data) {
            if (data) {
                $("#r-tip").hide();
                $("#showData").show();
                $("#r-title").show();
                $("#r-title").html("代码描述类别：" + codeClDsc);
                $("#sysCodeDsc_bnt").show();
                $(".r-item-tr").remove();
                var content = "";
                $.each(data, function (n, sysCodeDsc) {
                    var a = "<tr class='r-item-tr'>"
                        + "<td style='border-left: 1px solid #DCDCDC' class='ac'><input name='codeValues' value='" + sysCodeDsc.codeValue + "' type='text' data-validator='{required:true}' class='input-text' ></td>"
                        + "<td class='ac'><input name='codeDscs' value='" + sysCodeDsc.codeDsc + "' type='text' data-validator='{required:true}' class='input-text' ></td>";
                    var b = "";
                    if (sysCodeDsc.wthrDefault == 0) {
                        b = "<td class='ac'><select name='wthrDefaults' data-validator='{required:true}' class='select-box  select-box2' ><option value='0' selected='selected'>否</option><option value='1'>是</option></select></td>";
                    } else {
                        b = "<td class='ac'><select name='wthrDefaults' data-validator='{required:true}' class='select-box  select-box2' ><option value='0'>否</option><option value='1' selected='selected'>是</option></select></td>";
                    }
                    var c = "";
                    if (sysCodeDsc.enabledSt == 0) {
                        c = "<td class='ac'><select name='enabledSts' data-validator='{required:true}' class='select-box  select-box2' ><option value='0' selected='selected'>停用</option><option value='1'>启用</option></select></td>";
                    } else {
                        c = "<td class='ac'><select name='enabledSts' data-validator='{required:true}' class='select-box  select-box2' ><option value='0'>停用</option><option value='1' selected='selected'>启用</option></select></td>";
                    }
                    var d = "<td class='ac'>"
                        + "<a class='operate operate-delete' href='javascript:;'>删除</a>"
                        + "</td>"
                        + "</tr>";
                    content = content + a + b + c + d;
                });
                $("#r-th").after(content);
            } else {
                $("#r-tip").show();
                $("#r-tip").html("无数据。");
                $("#showData").hide();
                $("#r-title").hide();
                $("#sysCodeDsc_bnt").hide();
            }
        });
    };

    $right.on("click.addRowA", "#addRowA", function () { // 添加行的点击事件
        var content = "<tr class='r-item-tr'>"
            + "<td class='ac'><input name='codeValues' type='text' data-validator='{required:true}' class='input-text' ></td>"
            + "<td class='ac'><input name='codeDscs' type='text' data-validator='{required:true}' class='input-text' ></td>"
            + "<td class='ac'><select name='wthrDefaults' data-validator='{required:true}' class='select-box  select-box2' ><option value='0' selected='selected'>否</option><option value='1'>是</option></select></td>"
            + "<td class='ac'><select name='enabledSts' data-validator='{required:true}' class='select-box  select-box2' ><option value='0'>停用</option><option value='1' selected='selected'>启用</option></select></td>"
            + "<td class='ac'>"
            + "<a class='operate operate-delete' href='javascript:;'>删除</a>"
            + "</td>"
            + "</tr>";
        if ($(".r-item-tr").size() > 0) {
            $(".r-item-tr").last().after(content);
        } else {
            $("#r-th").after(content);
        }
    }).on("click.delRowA", ".operate-delete", function () { // 删除行的点击事件
        $(this).closest('tr').remove();
    }).on("click.update", "#sysCodeDsc_update", function () { // 保存
        var result = $.validator($sysCodeDscForm);
        if (result) {
            $.messager.confirm("是否保存", "是否保存代码描述信息？", function (r) {
                if (r) {
                    $.ajaxExt({
                        url: "/sysCodeDsc/uptSysCodeDsc.htm",
                        data: $sysCodeDscForm.serializeArray()
                    }).done(function (data) {
                        initRight();
                        $.messager.alert("提示", "代码描述信息保存成功。", "info");
                    });
                }
            });
        }
    }).on("click.reset", "#sysCodeDsc_reset", function () { // 重置
        initRight();
    }).on("click.reCashe", "#sysCodeDsc_reCashe", function () { // 保存
        $.messager.confirm("确认", "是否刷新代码缓存？", function (r) {
            if (r) {
                $.ajaxExt({
                    url: "/sysCodeDsc/reCasheSysCodeDsc.htm",
                    data: $sysCodeDscForm.serializeArray()
                }).done(function (data) {
                    $.messager.alert("提示", "刷新代码缓存成功,重新登录系统后生效!", "info");
                });
            }
        });
    });

    initInterface();

});